/*
 * Copyright 2007 - The JDPF Project Team (http://www.jdpf.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdpf.plugins.graphics.netloadergui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdpf.core.contextmanager.ContextManager;
import org.jdpf.core.contextmanager.services.View;
import org.jdpf.core.kernel.execution.Task;
import org.jdpf.core.plugins.parameters.Parameters;

@SuppressWarnings("serial")
public class NetLoaderGuiPanel extends JPanel implements View{

	private JButton btnRun, btnExit, 
	btnChooseNet, btnChooseParam;
	private JTextField txtNetFile, txtParamFile;
	private ContextManager manager;
	private File lastPath;
	private static final String TITLE="Net loader interface";
	private static final String INSTRUCTION="Select an XML file that " +
	"describes a JDPF net";
	private static final int WIDTH=600;
	private static final int HEIGHT=450;

	public NetLoaderGuiPanel(ContextManager manager){
		super(new BorderLayout());
		this.manager=manager;
		this.manager.registerView(this);
		setSize(new Dimension(WIDTH,HEIGHT));
		add(createLayout(),BorderLayout.CENTER);
	}

	private Box createLayout(){
		Box boxMain = Box.createVerticalBox();
		Box boxRow1 = Box.createHorizontalBox();
		Box boxRow2 = Box.createHorizontalBox();
		Box boxRow3 = Box.createHorizontalBox();

		btnRun = createButton("Run");
		btnRun.setEnabled(false);
		btnExit = createButton("Exit");
		btnChooseNet = createButton("Load Net...");
		btnChooseParam = createButton("Load Parameters...");
		btnChooseParam.setEnabled(false);
		txtNetFile = createTextField("");
		txtParamFile = createTextField("");
		JLabel lab1=new JLabel("Net file:");
		JLabel lab2=new JLabel("Parameters file:");
		
		
		btnRun.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				manager.runCurrentTask();
				manager.setCurrentTask(null);
			}
		});

		btnExit.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				manager.exit();
			}
		});

		btnChooseNet.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser;
				if (lastPath==null)
				    chooser = new JFileChooser();
				else
				    chooser = new JFileChooser(lastPath);
				
				chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				chooser.setFileFilter(new XMLFilter());
				chooser.showOpenDialog(NetLoaderGuiPanel.this);
				File f = chooser.getSelectedFile();
				lastPath = f;
				if(f!=null){		
					manager.chooseXMLNetFile(f.toString());	
					manager.setCurrentNet(manager.loadNet(f.toString()));
				}
				update();
			}
		});

		btnChooseParam.addActionListener(new AbstractAction(){
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser;
				if (lastPath==null)
				    chooser = new JFileChooser();
				else
				    chooser = new JFileChooser(lastPath);
				
				chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				chooser.setFileFilter(new XMLFilter());
				chooser.showOpenDialog(NetLoaderGuiPanel.this);
				File f = chooser.getSelectedFile();
				lastPath = f;
				if(f!=null){		
					manager.chooseXMLParamFile(f.toString());
					Task t=new Task(manager.getXMLNetFile(),f.toString());
					t.setNet(manager.getCurrentNet());
					manager.loadParam(t);
					t.setParam((Parameters) manager.getToken().getValueList().get(0));
					manager.setCurrentTask(t);
				}
				update();
			}
		});

		boxRow1.add(Box.createHorizontalGlue());
		boxRow1.add(lab1);
		boxRow1.add(Box.createHorizontalStrut(5));
		boxRow1.add(txtNetFile);
		boxRow1.add(Box.createHorizontalStrut(10));
		boxRow1.add(btnChooseNet);
		boxRow1.add(Box.createHorizontalGlue());

		boxRow2.add(Box.createHorizontalGlue());
		boxRow2.add(btnRun);
		boxRow2.add(Box.createHorizontalStrut(10));
		boxRow2.add(btnExit);
		boxRow2.add(Box.createHorizontalGlue());

		boxRow3.add(Box.createHorizontalGlue());
		boxRow3.add(lab2);
		boxRow3.add(Box.createHorizontalStrut(5));
		boxRow3.add(txtParamFile);
		boxRow3.add(Box.createHorizontalStrut(10));
		boxRow3.add(btnChooseParam);
		boxRow3.add(Box.createHorizontalGlue());		

		boxMain.add(Box.createVerticalGlue());
		boxMain.add(boxRow1);
		boxMain.add(Box.createVerticalStrut(10));
		boxMain.add(boxRow3);
		boxMain.add(Box.createVerticalStrut(10));
		boxMain.add(boxRow2);
		boxMain.add(Box.createVerticalGlue());

		return boxMain;
	}

	private JButton createButton(String label){
		JButton button = new JButton(label);
		button.setPreferredSize(button.getPreferredSize());
		return button;
	}

	private JTextField createTextField(String text){
		JTextField txt = new JTextField(text);
		txt.setMaximumSize(txt.getPreferredSize());
		txt.setColumns(25);
		return txt;
	}

	public void update() {
		btnRun.setEnabled(manager.getCurrentTask()!=null);
		btnChooseParam.setEnabled(manager.getCurrentNet()!=null);
		this.repaint();
	}

	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2=(Graphics2D) g;
		Font f=new Font("Helvetia",Font.BOLD,16);
		FontMetrics fm=g2.getFontMetrics(f);
		int x=(this.getWidth()-fm.stringWidth(TITLE))/2;
		int y=fm.getHeight()+20;
		g2.setFont(f);
		g2.drawString(TITLE,x,y);
		f=new Font("Helvetia",Font.PLAIN,12);
		fm=g2.getFontMetrics(f);
		x=(this.getWidth()-fm.stringWidth(INSTRUCTION))/2;
		y=fm.getHeight()+45;
		g2.setFont(f);
		g2.drawString(INSTRUCTION,x,y);
		String s=null;
		if(manager.getCurrentNet()!=null){
			s="Loaded net file:"+manager.getXMLNetFile();
			x=(this.getWidth()-fm.stringWidth(s))/2;
			g2.drawString(s,x,this.getY()+(this.getHeight()-50));
		}
		if(manager.getCurrentTask()!=null){
			s="Loaded parameters file:"+manager.getXMLParamFile();
			x=(this.getWidth()-fm.stringWidth(s))/2;
			g2.drawString(s,x,this.getY()+(this.getHeight()-35));
		}
		txtNetFile.setText(manager.getXMLNetFile());
		txtParamFile.setText(manager.getXMLParamFile());
	}
}
